<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="账号名称">
              <a-input placeholder="请输入账号名称" v-model="queryParam.name"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="1688账号ID">
              <a-input placeholder="请输入1688账号ID" v-model="queryParam.platformAccountId"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="使用人">
              <j-url-select
                v-model="queryParam.holderUsers"
                placeholder="请选择使用人"
                url="/sys/user/getAllUsernameByOrg"
              />
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="授权状态">
              <j-search-select-tag-new
                placeholder="请选择授权状态"
                v-model="queryParam.authStatus"
                dictCode="purchase_account_auth"
              />
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="使用状态">
              <j-search-select-tag-new
                placeholder="请选择使用状态"
                v-model="queryParam.enableStatus"
                dictCode="is_enable"
              />
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button
        v-has="'SettingAuthPurchase1688List:add'"
        type="primary"
        icon="plus"
        @click="
          editId = ''
          parentId = ''
          showEditModal = true
        "
        >新增1688授权</a-button
      >
    </div>

    <!-- table区域-begin -->
    <a-table
      :scroll="{ x: true }"
      bordered
      rowKey="id"
      :columns="columns"
      :dataSource="dataSource"
      :pagination="ipagination"
      :loading="loading"
    >
      <!-- 账号名称 -->
      <template #nameSlot="text, record">
        <a-tag v-if="record.parentId" color="purple">子</a-tag>
        <a-tag v-else color="blue">主</a-tag>
        {{ record.name }}
      </template>
      <template #platformAccountIdSlotSlot="text, record">
        <div>{{ record.platformAccountId }}</div>
        <div v-if="record.parentId">主账号：{{ record.parentId_dictText }}</div>
      </template>
      <!-- 授权状态 -->
      <template #authStatusSlot="text, record">
        <a-tag v-if="record.authStatus === 'unauthorized'" color="orange">未授权</a-tag>
        <a-tag v-else-if="record.authStatus === 'success'" color="green">授权成功</a-tag>
        <a-tag v-else-if="record.authStatus === 'expire'" color="purple">授权过期</a-tag>
        <a-tooltip v-else-if="record.authStatus === 'error'">
          <template slot="title"> {{ record.errorMsg }} </template>
          <a-tag color="red">授权失败</a-tag>
        </a-tooltip>
      </template>
      <!-- 使用状态 -->
      <template #enableStatusSlot="text, record">
        <a-tag v-if="record.enableStatus === 'Y'" color="green">启用</a-tag>
        <a-tag v-else color="red">停用</a-tag>
      </template>
      <!-- 授权时间 -->
      <template slot="authTimeSlot" slot-scope="text, record">
        <div class="templateSlot">
          <div v-if="record.authBeginTime">
            <span>开始：</span>
            <span>{{ record.authBeginTime }}</span>
          </div>
          <div v-if="record.authEndTime">
            <span>到期：</span>
            <span>{{ record.authEndTime }}</span>
          </div>
        </div>
      </template>
      <span slot="action" slot-scope="text, record">
        <a
          v-has="'SettingAuthPurchase1688List:edit'"
          @click="
            editId = record.id
            parentId = ''
            showEditModal = true
          "
          >编辑</a
        >

        <a-divider type="vertical" />
        <a-dropdown>
          <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
          <a-menu slot="overlay">
            <a-menu-item v-has="'SettingAuthPurchase1688List:edit'">
              <a @click="handleAuth(record)">重新授权</a>
            </a-menu-item>
            <a-menu-item v-has="'SettingAuthPurchase1688List:addChild'" v-if="!record.parentId">
              <a @click="clickAddSub(record)">授权子账号</a>
            </a-menu-item>
            <a-menu-item v-has="'SettingAuthPurchase1688List:log'">
              <j-operate-log :id="record.id" />
            </a-menu-item>
          </a-menu>
        </a-dropdown>
      </span>
    </a-table>

    <purchase-1688-modal
      v-if="showEditModal"
      :visible.sync="showEditModal"
      :editId="editId"
      :parentId="parentId"
      @submit="modalFormOk"
    />
  </a-card>
</template>

<script>
import { getAction } from '@/api/manage'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'

import Purchase1688Modal from './modules/Purchase1688Modal.vue'

export default {
  name: 'Purchase1688List',
  mixins: [JeecgListMixin],
  components: {
    Purchase1688Modal,
  },
  data() {
    return {
      showEditModal: false,
      editId: '', // 当前编辑的ID,
      parentId: '', // 当前新增的主账号id
      // 表头
      columns: [
        {
          title: '序号',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function (t, r, index) {
            return parseInt(index) + 1
          },
        },
        {
          title: '账号名称',
          align: 'center',
          dataIndex: 'name',
          scopedSlots: { customRender: 'nameSlot' },
        },
        {
          title: '1688账号ID',
          align: 'center',
          dataIndex: 'platformAccountId',
          scopedSlots: { customRender: 'platformAccountIdSlot' },
        },
        {
          title: '使用人',
          align: 'center',
          dataIndex: 'holderUsers',
          width: 200,
        },
        {
          title: '授权状态',
          align: 'center',
          dataIndex: 'authStatus_dictText',
          scopedSlots: { customRender: 'authStatusSlot' },
        },
        {
          title: '使用状态',
          align: 'center',
          dataIndex: 'enableStatus_dictText',
          scopedSlots: { customRender: 'enableStatusSlot' },
        },
        {
          title: '备注',
          align: 'center',
          dataIndex: 'remark',
          width: 250,
        },
        {
          title: '授权时间',
          align: 'center',
          dataIndex: 'authTime',
          scopedSlots: { customRender: 'authTimeSlot' },
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' },
        },
      ],
      url: {
        list: '/hzkj-purchase/purchase/auth/account/list',
        getAuthUrl: `/hzkj-purchase/purchase/auth/account/getAuthUrl`,
        getSubAccount: `/hzkj-purchase/purchase/auth/account/getSubAccount`,
      },
    }
  },
  methods: {
    clickAddSub({ authStatus, id }) {
      if (authStatus !== 'success') {
        this.$message.warning('请先完成主账号授权')
        return
      }
      getAction(this.url.getSubAccount, { id }).then((res) => {
        if (res.success) {
          if (res.result.length) {
            this.editId = ''
            this.parentId = id
            this.showEditModal = true
          } else {
            this.$message.warning('当前账号下无子账号，请确认所选账号是否正确。')
          }
        } else {
          this.$message.warning(res.message)
        }
      })
    },
    handleAuth({ authId, name }) {
      this.$confirm({
        content: (h) => {
          return h(
            'div',
            {
              domProps: {
                innerHTML: `<div style="padding-left: 40px;"><div style="font-weight: bold;">将直接跳转至1688后台授权</div><span style="color: red">${name}</span>你还要继续吗？</div>`,
              },
            },
            []
          )
        },
        onOk: () => {
          getAction(this.url.getAuthUrl, { authId }).then((res) => {
            if (res.success) {
              window.open(res.message)
            } else {
              this.$message.warning(res.message)
            }
          })
        },
      })
    },
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>
